%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify main_str to match path on your machine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main_str = 'C:\Users\Marco\Dropbox\Housing_Brainstorming\Information_disclosure';
% main_str = 'L:\agargano\Dropbox\Housing_Brainstorming\Information_disclosure';

save_str = [main_str,'\Replication_code_RFS\Model'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Some Characteristics

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Seller Reservation and Distribution of Private Values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
seller_reservation     = 650000;
bidders_mean           = 650000;
bidders_std            = 65000;
bidders_logstd         = sqrt(log((bidders_std^2)/exp(2*log(bidders_mean))+1));
bidders_logmean        = log(bidders_mean)-((bidders_logstd^2)/2);

n_sim                  = 1000000; % number of random draws for bids

start_bid_scale        = (0.75:0.01:1.25); 
start_bid              = start_bid_scale*seller_reservation; % vector of entry bids (1+alpha)*seller reservation
n_start                = size(start_bid,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Vector for number of bidders
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_bidders_all_vec      = (1:1:15); % number of bidders
i_all                  = size(n_bidders_all_vec,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Simulations

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Psales_mat      = nan(i_all,n_start);
Ebidders_mat    = nan(i_all,n_start);
EbidderW_mat    = nan(i_all,n_start);
Esales_mat      = nan(i_all,n_start);
Erev_mat        = nan(i_all,n_start);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
poolobj         = parpool('local');
% % tic;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parallel Loop over number of bidders
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parfor i = 1:i_all

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    start_bid_curr   = start_bid; % entry bid vector
    bid_values_mat   = lognrnd(bidders_logmean,bidders_logstd,n_bidders_all_vec(i),n_sim); % generate provate values
    if i > 1
         bid_values_mat = sort(bid_values_mat,'descend'); % sort (if more than one bidder)
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    for k = 1:n_start % loop over entry bids

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
        % Allocate memory for auction outcomes
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
        sales_succ_vec   = zeros(n_sim,1);
        bidder_value_vec = zeros(n_sim,1);
        sales_price_vec  = nan(n_sim,1);
        nbidders_vec     = zeros(n_sim,1);            
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
        for n = 1:n_sim % loop over draws of bidders private values
    
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            bids_values_vec       = bid_values_mat(:,n);       
            is_act                = bids_values_vec>start_bid_curr(k); % which bidders have private value above entry bid?

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
            nbidders_vec(n,1)     = sum(is_act); % num bidders with private value above entry bid (enter the auction)
                
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
            if nbidders_vec(n,1) == 1 % if only one bidder enters the auction
                 sales_price_vec(n,1)      = start_bid_curr(k); % sales price is entry bid
                 bidder_value_vec(n,1)     = bids_values_vec(1)-start_bid_curr(k); % surplus  
                 sales_succ_vec(n,1)       = 1; % house is sold   
            elseif nbidders_vec(n,1) > 1 % if multiple bidders enter auction   
                 sales_price_vec(n,1)      = bids_values_vec(2); % sales price is second highest bid
                 bidder_value_vec(n,1)     = bids_values_vec(1)-sales_price_vec(n,1); % surplus 
                 sales_succ_vec(n,1)       = 1; % house is sold   
            end    
            
        end
     
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        Psales_mat(i,k)    = sum(sales_succ_vec)/n_sim; % succesul sales / num sim
        Ebidders_mat(i,k)  = nanmean(nbidders_vec)/n_bidders_all_vec(i); % expected share of bidders entering the auction
        EbidderW_mat(i,k)  = nanmean(bidder_value_vec)/n_bidders_all_vec(i); % expected bidder surpls
        Esales_mat(i,k)    = nanmean(sales_price_vec)-seller_reservation; % expected sales price - minus seller reservation value
        Erev_mat(i,k)      = Psales_mat(i,k)*Esales_mat(i,k); % expected seller revenue: P(Sales) * E[PRICE|Sales]       
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        
    end
    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% toc;
delete(poolobj);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Save

save([save_str,'\Output\Last\equilibrium_step1_last.mat']);

